package com.wt.springsamples.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wt.springsamples.biz.mybatis.mapper.SysMenuMapper;
import com.wt.springsamples.biz.mybatis.mapper.SysRoleFuncRefMapper;
import com.wt.springsamples.biz.mybatis.mapper.SysRoleMapper;
import com.wt.springsamples.biz.mybatis.mapper.SysRoleMenuRefMapper;
import com.wt.springsamples.common.BizException;
import com.wt.springsamples.model.*;
import com.wt.springsamples.biz.service.ISysRoleFunctionService;
import com.wt.springsamples.biz.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("sysRoleService")
@Transactional
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleFuncRefMapper sysRoleFuncRefMapper;

    @Autowired
    private SysRoleMenuRefMapper sysRoleMenuRefMapper;

    @Autowired
    private ISysRoleFunctionService  sysRoleFunctionService;

    @Override
    public boolean save(SysRole entity) {
        return super.save(entity);
    }

    public boolean update(SysRole entity) {
        if(entity.getId()==null){
            throw new BizException(5001,"id不能为空");
        }
        return super.updateById(entity);
    }

    @Override
    public boolean saveOrUpdate(SysRole entity) {
//        super.saveOrUpdate(entity);
        Wrapper<SysRole> updateWrapper = new UpdateWrapper();
        return super.saveOrUpdate(entity, updateWrapper);
    }

    @Override
    public void deleteById(Long id) {
//        super.removeById(id);
        sysRoleMapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        super.removeByIds(ids);
    }

    @Override
    public <T> Page<SysRole> selectPage(T condition, int pageIndex, int pageSize) {
        Page<SysRole> page= new Page(pageIndex,pageSize);
        page.addOrder(OrderItem.desc("create_time"));
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        return  super.page(page,queryWrapper);
//        return  sysRoleMapper.selectPage(page, queryWrapper);
    }

    @Transactional
    @Override
    public void config(Long roleId, List<Long> funcIdList) {
        SysRole role = sysRoleMapper.selectById(roleId);
        if(role==null){
            throw new BizException(5002,"角色不存在");
        }
        //TODO 验证权限是否存在
        List<SysRoleFuncRef> mappings = new ArrayList<>();
        for (Long funcId:funcIdList){
            SysRoleFuncRef mapping = new SysRoleFuncRef();
            mapping.setRoleId(roleId);
            mapping.setFuncId(funcId);
            mappings.add(mapping);
        }
        if(!CollectionUtils.isEmpty(mappings)){
            sysRoleFunctionService.saveBatch(mappings);
        }
    }

    @Override
    public void viewConfig(Long roleId, List<Long> viewIdList) {
        SysRole role = sysRoleMapper.selectById(roleId);
        if(role==null){
            throw new BizException(5002,"角色不存在");
        }

        //TODO 验证权限是否存在
        //删除角色原有数据
        QueryWrapper<SysRoleMenuRef> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        sysRoleMenuRefMapper.delete(wrapper);
        List<SysRoleMenuRef> mappings = new ArrayList<>();
        for (Long funcId:viewIdList){
            SysRoleMenuRef mapping = new SysRoleMenuRef();
            mapping.setRoleId(roleId);
            mapping.setMenuId(funcId);
            mappings.add(mapping);
        }
        if(!CollectionUtils.isEmpty(mappings)){
            super.saveBatch(SysRoleMenuRef.class,mappings);
        }
    }

    @Override
    public List<Long> getConfigMenuList(long roleId) {
        QueryWrapper<SysRoleMenuRef> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        List<SysRoleMenuRef> configs = sysRoleMenuRefMapper.selectList(wrapper);
        return  configs.stream().map((e)-> e.getMenuId()).collect(Collectors.toList());
    }
}
